﻿using Basic.FlowCollect;
using Basic.FlowExtendService;
using Basic.FlowModel;
using Basic.FlowModel.DB;
using Basic.FlowRemoteModel;
using Basic.FlowRemoteModel.FlowAppPower.Model;
using Basic.FlowService.Interface;
using Basic.HrRemoteModel.Power.Model;
using WeDonekRpc.Client;
using WeDonekRpc.Helper;

namespace Basic.FlowService.lmpl
{
    internal class FlowAppPowerService : IFlowAppPowerService
    {
        private readonly IFlowAppCollect _FlowApp;
        private readonly IPowerService _Power;
        private readonly IFlowAppPowerConfig _Config;

        public FlowAppPowerService ( IFlowAppCollect flowApp,
            IPowerService power,
            IFlowAppPowerConfig config )
        {
            this._FlowApp = flowApp;
            this._Power = power;
            this._Config = config;
        }
        public void Delete ( long appId )
        {
            this._Power.Delete(ConstDic.FlowAppLinkType, appId);
        }

        public AppPowerDto Get ( long appId )
        {
            PowerData dto = this._Power.Find(ConstDic.FlowAppLinkType, appId);
            if ( dto == null )
            {
                return null;
            }
            return dto.ConvertMap<PowerData, AppPowerDto>();
        }
        public void Save ( AppPowerSet data )
        {
            DBFlowApp app = this._FlowApp.Get(data.AppId);
            if ( app.AppType != FlowAppType.内部应用 )
            {
                throw new ErrorException("flow.app.type.error");
            }
            PowerSet add = new PowerSet
            {
                Description = data.Description,
                SubSystemId = data.SubSystemId,
                Icon = data.Icon,
                Name = data.Name,
                ParentId = data.ParentId,
                PowerType = data.PowerType,
                IsEnable = app.Status == FlowAppStatus.启用
            };
            if ( data.PowerType == HrRemoteModel.PowerType.menu )
            {
                add.PageParam = new Dictionary<string, string>
                {
                    {"id",app.Id.ToString() }
                };
                add.PagePath = this._Config.PagePath;
                add.RoutePath = this._Config.RoutePath;
                add.RouteName = this._Config.RouteName;
            }
            this._Power.Save(add, ConstDic.FlowAppLinkType, app.Id);
        }
    }
}
